<template>
  <div id="globalSider">
    <a-layout-sider class="sider" width="200" v-if="loginUserStore.loginUser.id"
    breakpoint="lg"
    collapsedWidth="0"
    >
      <a-menu mode="inline" v-model:selectedKeys="current" :items="items" @click="doMenuClick" />
    </a-layout-sider>
  </div>
</template>
<script lang="ts" setup>
import { type MenuProps } from 'ant-design-vue'
import { h, ref } from 'vue'
import { useRouter } from 'vue-router'
import { PictureOutlined, UserOutlined } from '@ant-design/icons-vue'
import { computed } from 'vue'
import { useLoginUserStore } from '../stores/useLoginUserStore'
const loginUserStore = useLoginUserStore()

const router = useRouter()

// 当前选中菜单
const current = ref<string[]>([])
// 监听路由变化，更新当前选中菜单
router.afterEach((to, from, failure) => {
  current.value = [to.path]
})

// 路由跳转事件
const doMenuClick = ({ key }: { key: string }) => {
  router.push({
    path: key,
  })
}

// 菜单列表
const originItems = [
  {
    key: '/',
    label: '公共图库',
    icon: () => h(PictureOutlined),
  },
  {
    key: '/my_space',
    label: '我的空间',
    icon: () => h(UserOutlined),
  },
]

// 过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    if (menu?.key?.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}

// 展示在菜单的路由数组
const items = computed<MenuProps['items']>(() => filterMenus(originItems))
</script>

<style scoped>


</style>
